using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;
using System.Net.Mail;

using Kypris2.Utils;
using Kypris2.CoreElements;
using Kypris2.CoreManagers;

using Jamila2.Database;

using GonzalesCore.CoreElements;
using GonzalesCore.CoreManagers;

namespace FileFactory
{
    #region FileFactory
    public class FileFactory
    {
        protected string DatabaseConnection = "";
        protected TypeOfDatabase DatabaseType = TypeOfDatabase.MSSQL;
        public ArrayList LoggedMessages = new ArrayList();
        //protected bool EmailActvie = false;
        //protected string SMTP = "";
        protected PersonalSessionAccount CurrentSystemSession = null;
        public DateTime UpdateDate = DateTime.MinValue;

        public FileFactory(PersonalSessionAccount PrivateConfig)
        {
            //this.ArchievedPath = CurConfigurator.PathForArchievedFiles;
            //this.LogPath = CurConfigurator.PathForLogFiles;
            //this.SourcePath = CurConfigurator.PathForSourceFiles;
            this.DatabaseConnection = GonzalesGeneralVariables.DatabaseConnectionString;
            this.DatabaseType = GonzalesGeneralVariables.DatabaseType;
            //this.EmailActvie = GonzalesGeneralVariables.EmailActive;
            //this.SMTP = GonzalesGeneralVariables.SMTP;
            this.CurrentSystemSession = PrivateConfig;
        }

        public bool StartWatching(Stream fileStream, string fileType, string Entity)
        {
            bool smooth = true;

            #region Analyze File

            switch (fileType)
            {
                case "R":
                    #region Exchange Rate File
                    using (ExRateFileFactory CurFileFactory = new ExRateFileFactory(fileStream, this.DatabaseConnection, this.DatabaseType, UpdateDate))
                    {
                        LoggedMessages.Add(DateTime.Today.ToShortDateString() + " " + DateTime.Today.ToShortTimeString() + " " + "*** Exchange Rate extract ***");
                        if (CurFileFactory.ReadFile())
                        {
                            LoggedMessages.Add(DateTime.Today.ToShortDateString() + " " + DateTime.Today.ToShortTimeString() + " " + "File extracted successfully.");
                        }
                        else
                        {
                            smooth = false;
                            LoggedMessages.Add(DateTime.Today.ToShortDateString() + " " + DateTime.Today.ToShortTimeString() + " " + "Error: " + CurFileFactory.ErrorMessage);
                        }
                    }
                    #endregion
                    break;
                case "E":
                    #region Employee Master
                    using (EmployeeFileFactory CurFileFactory = new EmployeeFileFactory(fileStream, this.DatabaseConnection, this.DatabaseType, Entity))
                    {
                        LoggedMessages.Add(DateTime.Today.ToShortDateString() + " " + DateTime.Today.ToShortTimeString() + " " + "*** Employees extract (" + Entity + ") ***");
                        if (CurFileFactory.ReadFile())
                        {
                            LoggedMessages.Add(DateTime.Today.ToShortDateString() + " " + DateTime.Today.ToShortTimeString() + " " + "File extracted successfully.");
                        }
                        else
                        {
                            smooth = false;
                            LoggedMessages.Add(DateTime.Today.ToShortDateString() + " " + DateTime.Today.ToShortTimeString() + " " + "Error: " + CurFileFactory.ErrorMessage);
                        }
                    }
                    #endregion
                    break;

                case "P":
                    #region Project Master
                    using (ProjectFileFactory CurFileFactory = new ProjectFileFactory(fileStream, this.DatabaseConnection, this.DatabaseType))
                    {
                        LoggedMessages.Add(DateTime.Today.ToShortDateString() + " " + DateTime.Today.ToShortTimeString() + " " + "*** Projects extract (" + Entity + ") ***");
                        if (CurFileFactory.ReadFile())
                        {
                            LoggedMessages.Add(DateTime.Today.ToShortDateString() + " " + DateTime.Today.ToShortTimeString() + " " + "File extracted successfully.");
                        }
                        else
                        {
                            smooth = false;
                            LoggedMessages.Add(DateTime.Today.ToShortDateString() + " " + DateTime.Today.ToShortTimeString() + " " + "Error: " + CurFileFactory.ErrorMessage);
                        }
                    }
                    #endregion
                    break;

                case "A": // expense details 
                    #region Expense Details
                    // expense detail currently only acept SG file only
                    //if (Entity.CompareTo("SG") == 0)
                    {
                        using (ExpenseDetailFactory CurFileFactory = new ExpenseDetailFactory(fileStream, this.DatabaseConnection, this.DatabaseType))
                        {
                            LoggedMessages.Add(DateTime.Today.ToShortDateString() + " " + DateTime.Today.ToShortTimeString() + " " + "*** Expense Detail extract (" + Entity + ") ***");
                            if (CurFileFactory.ReadFile())
                            {
                                LoggedMessages.Add(DateTime.Today.ToShortDateString() + " " + DateTime.Today.ToShortTimeString() + " " + "File extracted successfully.");
                            }
                            else
                            {
                                smooth = false;
                                LoggedMessages.Add(DateTime.Today.ToShortDateString() + " " + DateTime.Today.ToShortTimeString() + " " + "Error: " + CurFileFactory.ErrorMessage);
                            }
                        }
                    }
                    #endregion
                    break;

                case "C":
                    #region Cost Center
                    using (ProfitCenterFactory CurFileFactory = new ProfitCenterFactory(fileStream, this.DatabaseConnection, this.DatabaseType))
                    {
                        LoggedMessages.Add(DateTime.Today.ToShortDateString() + " " + DateTime.Today.ToShortTimeString() + " " + "*** Cost Center extract (" + Entity + ") ***");
                        if (CurFileFactory.ReadFile())
                        {
                            LoggedMessages.Add(DateTime.Today.ToShortDateString() + " " + DateTime.Today.ToShortTimeString() + " " + "File extracted successfully.");
                        }
                        else
                        {
                            smooth = false;
                            LoggedMessages.Add(DateTime.Today.ToShortDateString() + " " + DateTime.Today.ToShortTimeString() + " " + "Error: " + CurFileFactory.ErrorMessage);
                        }
                    }
                    #endregion
                    break;
            }
            #endregion

            #region Move file
            /*try
            {
                string NewFolderName = DateTime.Now.Month + "-" + DateTime.Now.Day + "-" + DateTime.Now.Year;

                if (!Directory.Exists(this.ArchievedPath + "\\" + NewFolderName))
                    Directory.CreateDirectory(this.ArchievedPath + "\\" + NewFolderName);

                File.Move(this.SourcePath + "\\" + CurFileInfo.Name, this.ArchievedPath + "\\" + NewFolderName + "\\" + CurFileInfo.Name + "-" + "-" + DateTime.Now.Hour + "-" + DateTime.Now.Minute);
            }
            catch (Exception ex)
            {
                smooth = false;
                LoggedMessages.Add(DateTime.Today.ToShortDateString() + " " + DateTime.Today.ToShortTimeString() + " " + " File moving : Exception : " + ex.Message);
            }*/
            #endregion

            //Check the master users table if there's empty email and empty ad distinguished name send email to super admin
            /*UserManager curManager = new UserManager(this.CurrentSystemSession);
            DataTable dtMasterUsers = curManager.GetUsersWithoutEmailAndADDistinguishedName();
            string MailContent = "", MailTitle = "";

            if (dtMasterUsers != null && dtMasterUsers.Rows.Count > 0)
            {
                #region generate content and send email to Person in charge
                if (this.EmailActvie)
                {
                    MailTitle = "Claimants without Email or AD Distinguished Name in Ezyclaim";
                    MailContent += "<table><tr><td>Employee Name</td><td>Employee ID</td></tr>";

                    for(int i =0; i < dtMasterUsers.Rows.Count; i++)
                    {
                        MailContent += "<tr><td>" + dtMasterUsers.Rows[i][2].ToString() + "</td><td>" + dtMasterUsers.Rows[i][1].ToString() + "</td></tr>";
                    }
                    MailContent += "</table>";

                    if (MailContent.Length > 0)
                    {
                        SystemUsers curAdmin = null;

                        using (UserManager CurUserManager = new UserManager(this.CurrentSystemSession))
                        {
                            curAdmin = CurUserManager.GetAdminPanel();

                            if (curAdmin != null && curAdmin.Count > 0)
                            {
                                for (int x = 0; x < curAdmin.Count; x++)
                                {
                                    using (EmailFactory CurEmailFactory = new EmailFactory(
                                        curAdmin[x].DisplayName,
                                        curAdmin[x].Email,
                                        MailTitle,
                                        MailContent))
                                     {
                                        if (CurEmailFactory.IsReady)
                                        {
                                            if (!CurEmailFactory.SendNow())
                                            {
                                                LoggedMessages.Add(DateTime.Today.ToShortDateString() + " " + DateTime.Today.ToShortTimeString() + " " + " Sending Email(Missing info) : Exception : " + CurEmailFactory.ErrorMessage);
                                            }
                                            else
                                            {
                                                LoggedMessages.Add(DateTime.Today.ToShortDateString() + " " + DateTime.Today.ToShortTimeString() + " " + " Sending Email(Missing info) : Email sent to " + curAdmin[x].Email);
                                            }
                                        }
                                        else
                                        {
                                            LoggedMessages.Add(DateTime.Today.ToShortDateString() + " " + DateTime.Today.ToShortTimeString() + " " + " Sending Email(Missing info) : Exception : " + CurEmailFactory.ErrorMessage);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                #endregion
            }

            GenerateClaimReminder();

            GenerateCreditCardReminder();*/

            return smooth;
        }

        #region GenerateCreditCardReminder
        public bool GenerateCreditCardReminder()
        {
            bool smooth = true;

            /*using (CompanyManager CurCompanyManager = new CompanyManager(this.CurrentSystemSession))
            {
                GonzalesCompanies CompanyCollection = CurCompanyManager.GetAllCompany();

                if (CompanyCollection != null)
                {
                    LoggedMessages.Add("*** ECLAIMCREDITCARDREMINDER ***");

                    for (int z = 0; z < CompanyCollection.Count; z++)
                    {
                        int CreditCardReminder = Convert.ToInt32(CompanyCollection[z].CreditCardReminder);

                        if (CreditCardReminder > 0)
                        {
                            GonzalesCompany curCompany = new GonzalesCompany(CompanyCollection[z].InternalID, CompanyCollection[z].CompanyName);
                            CreditCardManager CurCreditCardManager = new CreditCardManager(this.CurrentSystemSession);
                            DataTable CreditCardCollection = CurCreditCardManager.GetAvailableCreditCardItemByCompany(CompanyCollection[z].InternalID, false);

                            try
                            {
                                if (CreditCardCollection.Rows.Count > 0)
                                {
                                    LoggedMessages.Add("Extract the Credit Card Item based on Company");

                                    for (int x = 0; x < CreditCardCollection.Rows.Count; x++)
                                    {
                                        DateTime Transaction_Date = Convert.ToDateTime(CreditCardCollection.Rows[x]["trans_Date"]).AddDays(CreditCardReminder);

                                        if (Transaction_Date <= DateTime.Now)
                                        {
                                            #region Generate Email to approver and user.

                                            #endregion
                                        }

                                    }

                                }
                                else
                                {
                                    LoggedMessages.Add("File Failed for " + CompanyCollection[z].Entity.InternalID + CompanyCollection[z].CompanyCode + " : " + CurCreditCardManager.ErrorMessage);
                                }
                            }
                            catch (Exception ex)
                            {
                                LoggedMessages.Add("File Failed for " + CompanyCollection[z].Entity.InternalID + CompanyCollection[z].CompanyCode + " : Exception : " + ex.Message);
                            }
                        }
                    }
                }
            } */

            return smooth;
        }
        #endregion
        #region GenerateClaimReminder
        public bool GenerateClaimReminder()
        {
            bool smooth = true;

            /*using (CompanyManager CurCompanyManager = new CompanyManager(this.CurrentSystemSession))
            {
                GonzalesCompanies CompanyCollection = CurCompanyManager.GetAllCompany();

                if (CompanyCollection != null)
                {
                    LoggedMessages.Add("*** ECLAIMCLAIMREMINDER ***");

                    for (int z = 0; z < CompanyCollection.Count; z++)
                    {
                        int ClaimReminder = Convert.ToInt32(CompanyCollection[z].ClaimReminder);

                        GonzalesCompany curCompany = new GonzalesCompany(CompanyCollection[z].InternalID, CompanyCollection[z].CompanyName);
                        ClaimManager CurClaimManager = new ClaimManager(this.CurrentSystemSession);
                        ClaimHeaders ClaimCollection = CurClaimManager.GetClaimByStatus(GonzalesClaimHeader.TypeOfClaimStatus.Submitted, curCompany);

                        if (ClaimReminder > 0)
                        {
                            try
                            {

                                if (ClaimCollection != null)
                                {
                                    LoggedMessages.Add("Extract the Claim based on Company");

                                    for (int x = 0; x < ClaimCollection.Count; x++)
                                    {

                                        if (ClaimCollection[x].SubmittedOn.AddDays(ClaimReminder) <= (DateTime.Now) && ClaimCollection[x].ClaimID.Length > 0)
                                        {
                                            #region Generate Email to approver and user.
                                            TimeSpan datediff = DateTime.Now.Subtract(ClaimCollection[x].SubmittedOn);
                                            string MailContent = "", MailTitle = "";

                                            if (this.EmailActvie)
                                            {
                                                #region generate content and send email to Approver
                                                using (EmailGenerator CurMailGenerator = new EmailGenerator(Path.GetFullPath("MailContent\\") + "ReminderClaimForApprover", "en"))
                                                {
                                                    MailContent = CurMailGenerator.GenerateReminderClaimForApprover(ClaimCollection[x].ClaimID, datediff.Days.ToString());

                                                    MailTitle = CurMailGenerator.MailTitle;
                                                }

                                                if (MailContent.Length > 0)
                                                {
                                                    using (EmailFactory CurEmailFactory = new EmailFactory(
                                                        ClaimCollection[x].NextApprover.DisplayName,
                                                        ClaimCollection[x].NextApprover.Email,
                                                        MailTitle,
                                                        MailContent))
                                                    {
                                                        if (CurEmailFactory.IsReady)
                                                        {
                                                            if (!CurEmailFactory.SendNow())
                                                            {
                                                                LoggedMessages.Add(DateTime.Today.ToShortDateString() + " " + DateTime.Today.ToShortTimeString() + " " + " Sending Email(reminder to approval) : Exception : " + CurEmailFactory.ErrorMessage);
                                                            }
                                                            else
                                                            {
                                                                LoggedMessages.Add(DateTime.Today.ToShortDateString() + " " + DateTime.Today.ToShortTimeString() + " " + " Sending Email(reminder to approval) : Email sent to " + ClaimCollection[x].NextApprover.Email);
                                                            }
                                                        }
                                                        else
                                                        {
                                                            LoggedMessages.Add(DateTime.Today.ToShortDateString() + " " + DateTime.Today.ToShortTimeString() + " " + " Sending Email(reminder to approval) : Exception : " + CurEmailFactory.ErrorMessage);
                                                        }
                                                    }
                                                }
                                                #endregion

                                                #region generate content and send email to claimant
                                                using (EmailGenerator CurMailGenerator = new EmailGenerator(Path.GetFullPath("MailContent\\") + "ReminderClaimForClaimant", "en"))
                                                {
                                                    MailContent = CurMailGenerator.GenerateReminderClaimForClaimant(ClaimCollection[x].ClaimID, datediff.Days.ToString());

                                                    MailTitle = CurMailGenerator.MailTitle;
                                                }

                                                if (MailContent.Length > 0)
                                                {
                                                    using (EmailFactory CurEmailFactory = new EmailFactory(
                                                        ClaimCollection[x].ClaimedBy.DisplayName,
                                                        ClaimCollection[x].ClaimedBy.Email,
                                                        MailTitle,
                                                        MailContent))
                                                    {
                                                        if (CurEmailFactory.IsReady)
                                                        {
                                                            if (!CurEmailFactory.SendNow())
                                                            {
                                                                LoggedMessages.Add(DateTime.Today.ToShortDateString() + " " + DateTime.Today.ToShortTimeString() + " " + " Sending Email(reminder to claimant) : Exception : " + CurEmailFactory.ErrorMessage);
                                                            }
                                                            else
                                                            {
                                                                LoggedMessages.Add(DateTime.Today.ToShortDateString() + " " + DateTime.Today.ToShortTimeString() + " " + " Sending Email(reminder to claimant) : Email sent to " + ClaimCollection[x].ClaimedBy.Email);
                                                            }
                                                        }
                                                        else
                                                        {
                                                            LoggedMessages.Add(DateTime.Today.ToShortDateString() + " " + DateTime.Today.ToShortTimeString() + " " + " Sending Email(reminder to claimant) : Exception : " + CurEmailFactory.ErrorMessage);
                                                        }
                                                    }
                                                }
                                                #endregion
                                            }
                                            #endregion
                                        }

                                    }

                                }
                                else
                                {
                                    LoggedMessages.Add("File Failed for " + CompanyCollection[z].Entity.InternalID + CompanyCollection[z].CompanyCode + " : " + CurClaimManager.ErrorMessage);
                                }
                            }
                            catch (Exception ex)
                            {
                                LoggedMessages.Add("File Failed for " + CompanyCollection[z].Entity.InternalID + CompanyCollection[z].CompanyCode + " : Exception : " + ex.Message);
                            }
                        }
                    }
                }
            }*/

            return smooth;
        }
        #endregion
    }
    #endregion

    #region EmployeeFileFactory
    public class EmployeeFileFactory : FileFactoryBase
    {
        SystemUsers UsersCollection = null;
        protected readonly int LineLength = 9;

        public EmployeeFileFactory(Stream fileStream, string DBConnectionString, TypeOfDatabase DatabaseType, string Entity)
            : base(fileStream, DBConnectionString, DatabaseType)
        {
            base.FileStreamObj = fileStream;
            base.DBConnectionString = DBConnectionString;
            base.DatabaseType = DatabaseType;
            base.EntityID = Entity;
        }

        public override bool ReadFile()
        {
            bool smooth = true;

            if ((smooth = base.ReadFile()))
            {
                if (this.FileLines != null)
                {
                    this.UsersCollection = new SystemUsers();

                    using (RawEmployeeDataManager CurDataManager = new RawEmployeeDataManager(this.CurrentSystemSession))
                    {
                        // execute pre-action before inject data
                        CurDataManager.ExecutePreInjectAction();
                        // start to analyze
                        for (int x = 1; x < this.FileLines.Count; x++) // x = 0 these is header file
                        {
                            try
                            {
                                string[] CurrentLine = this.FileLines[x].ToString().Split(',');
                                // break by offset
                                if (CurrentLine.Length >= this.LineLength)  // 9 is the only items
                                {
                                    string CountryID = CurrentLine[0];    // size: 2
                                    string CompanyCode = CurrentLine[1];  // size: 2
                                    string EmployeeID = CurrentLine[2];   // size: 6
                                    string ActualName = CurrentLine[3]; // size: 50
                                    string LocalName = CurrentLine[4];
                                    string EmailID = CurrentLine[5];    // size: 50
                                    string CostCenter = CurrentLine[6];// size: 10
                                    string Email = CurrentLine[7];     // size: 60
                                    string VendorID = CurrentLine[8];   // size: 15
                                    
                                    CurDataManager.AddData(this.EntityID, CountryID, CompanyCode, EmployeeID, ActualName, LocalName, EmailID, CostCenter, Email, VendorID);
                                }
                            }
                            catch (Exception ex)
                            {
                                smooth = false;
                                this.ErrMsg = "[EmployeeFileFactory.ReadFile] : Exception : " + ex.Message;
                            }
                        }   // for

                        // inject now
                        if (CurDataManager.TotalInTheList > 0)
                        {
                            CurDataManager.ExecuteFinalAction();

                            if (!CurDataManager.ExecuteDataInject())
                            {

                                smooth = false;
                                this.ErrMsg = "[EmployeeFileFactory.ReadFile] : CurDataManager.ExecuteDataInject() : " + CurDataManager.ErrorMessage;

                            }
                        }
                    }
                }
            }
            return smooth;
        }
    }
    #endregion

    #region ExpenseDetailFactory
    public class ExpenseDetailFactory : FileFactoryBase
    {
        SystemUsers UsersCollection = null;
        protected readonly int LineLength = 3;

        public ExpenseDetailFactory(Stream fileStream, string DBConnectionString, TypeOfDatabase DatabaseType)
            : base(fileStream, DBConnectionString, DatabaseType)
        {
            base.FileStreamObj = fileStream;
            base.DBConnectionString = DBConnectionString;
            base.DatabaseType = DatabaseType;
        }

        public override bool ReadFile()
        {
            bool smooth = true;

            if ((smooth = base.ReadFile()))
            {
                if (this.FileLines != null)
                {
                    this.UsersCollection = new SystemUsers();

                    using (RawExpenseDetailManager CurDataManager = new RawExpenseDetailManager(this.CurrentSystemSession))
                    {
                        // execute pre-action before inject data
                        CurDataManager.ExecutePreInjectAction();
                        // start to analyze
                        for (int x = 1; x < this.FileLines.Count; x++) // x = 0 these is header file
                        {
                            try
                            {
                                string[] CurrentLine = this.FileLines[x].ToString().Split(',');
                                // break by offset
                                if (CurrentLine.Length >= this.LineLength)  // 3 is the only length
                                {
                                    string EntityID = CurrentLine[0];    // size: 2
                                    string AccountCode = CurrentLine[1];    // size: 20
                                    string Description = (CurrentLine[2].Length > 100) ? CurrentLine[2].Substring(0, 100).Trim() : CurrentLine[2];    // size: 100
                                    CurDataManager.AddData(EntityID, AccountCode, Description);
                                }
                            }
                            catch (Exception ex)
                            {
                                smooth = false;
                                this.ErrMsg = "[ExpenseDetailFactory.ReadFile] : Exception : " + ex.Message;
                            }
                        }   // for

                        // inject now
                        if (CurDataManager.TotalInTheList > 0)
                        {
                            CurDataManager.ExecuteFinalAction();

                            if (!CurDataManager.ExecuteDataInject())
                            {
                                smooth = false;
                                this.ErrMsg = "[ExpenseDetailFactory.ReadFile] : CurDataManager.ExecuteDataInject() : " + CurDataManager.ErrorMessage;
                            }
                        }
                    }
                }
            }

            return smooth;
        }
    }
    #endregion

    #region ExRateFileFactory
    public class ExRateFileFactory : FileFactoryBase
    {
        SystemUsers UsersCollection = null;
        protected readonly int LineLength = 2;
        protected static DateTime UpdateDate = DateTime.MinValue;
        
        public ExRateFileFactory(Stream fileStream, string DBConnectionString, TypeOfDatabase DatabaseType, DateTime adt)
            : base(fileStream, DBConnectionString, DatabaseType)
        {
            UpdateDate = adt;
            base.FileStreamObj = fileStream;
            base.DBConnectionString = DBConnectionString;
            base.DatabaseType = DatabaseType;
        }

        #region ReadFile
        public override bool ReadFile()
        {
            bool smooth = true;

            if ((smooth = base.ReadFile()))
            {
                if (this.FileLines != null)
                {
                    this.UsersCollection = new SystemUsers();

                    using (RawExRateDataManager CurDataManager = new RawExRateDataManager(this.CurrentSystemSession))
                    {
                        if (this.TryConnection())
                        {
                            // execute pre-action before inject data
                            //CurDataManager.ExecutePreInjectAction();

                            // start to analyze
                            for (int x = 1; x < this.FileLines.Count; x++) // x = 0: these is header file
                            {
                                try
                                {
                                    string[] CurrentLine = this.FileLines[x].ToString().Split(',');
                                    // break by offset
                                    if (CurrentLine.Length >= this.LineLength)  // 2 is the only two items
                                    {
                                        string Currency = CurrentLine[0];
                                        string ExRateInStr = CurrentLine[1];
                                        decimal ExRate = Convert.ToDecimal(ExRateInStr);
                                        CurDataManager.AddData(Currency, ExRate, UpdateDate);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    smooth = false;
                                    this.ErrMsg = "[ExRateFileFactory.ReadFile] : Exception : " + ex.Message;
                                }
                            }   // for

                            // inject now
                            if (CurDataManager.TotalInTheList > 0)
                            {
                                CurDataManager.ExecuteFinalAction(UpdateDate);
                                smooth = CurDataManager.ExecuteDataInject();
                                if (!smooth)
                                {
                                    this.ErrMsg = "[ExRateFileFactory.ReadFile] : CurDataManager.ExecuteDataInject() : " + CurDataManager.ErrorMessage;
                                }
                            }
                        }
                        else
                        {
                            smooth = false;
                        }
                    }
                }
            }
            return smooth;
        }
        #endregion
    }
    #endregion

    #region ProjectFileFactory
    public class ProjectFileFactory : FileFactoryBase
    {
        SystemUsers UsersCollection = null;
        protected readonly int LineLength = 4;

        public ProjectFileFactory(Stream fileStream, string DBConnectionString, TypeOfDatabase DatabaseType)
            : base(fileStream, DBConnectionString, DatabaseType)
        {
            base.FileStreamObj = fileStream;
            base.DBConnectionString = DBConnectionString;
            base.DatabaseType = DatabaseType;
        }

        public override bool ReadFile()
        {
            bool smooth = true;

            if ((smooth = base.ReadFile()))
            {
                if (this.FileLines != null)
                {
                    this.UsersCollection = new SystemUsers();

                    using (RawProjectManager CurDataManager = new RawProjectManager(this.CurrentSystemSession))
                    {
                        // execute pre-action before inject data
                        CurDataManager.ExecutePreInjectAction();

                        // start to analyze
                        for (int x = 1; x < this.FileLines.Count; x++) // x = 0 these is header file
                        {
                            try
                            {
                                string[] CurrentLine = this.FileLines[x].ToString().Split(',');
                                // break by offset
                                if (CurrentLine.Length >= this.LineLength)  // 4 is the only items
                                {
                                    string EntityID = CurrentLine[0];    // size: 2
                                    string ProjectID = CurrentLine[1];    // size: 10
                                    string ProjectDescription = CurrentLine[2];    // size: 50
                                    string ProjectType = CurrentLine[3];      // size: 1

                                    CurDataManager.AddData(EntityID, ProjectID, ProjectDescription, ProjectType);
                                }
                            }
                            catch (Exception ex)
                            {
                                smooth = false;
                                this.ErrMsg = "[ExRateFileFactory.ReadFile] : Exception : " + ex.Message;
                            }
                        }   // for

                        // inject now

                        if (CurDataManager.TotalInTheList > 0)
                        {
                            CurDataManager.ExecuteFinalAction();

                            if (!CurDataManager.ExecuteDataInject())
                            {
                                smooth = false;
                                this.ErrMsg = "[ProjectFileFactory.ReadFile] : CurDataManager.ExecuteDataInject() : " + CurDataManager.ErrorMessage;

                            }
                        }

                    }
                }
            }

            return smooth;
        }
    }
    #endregion

    #region Profit Center/ Cost Center
    public class ProfitCenterFactory : FileFactoryBase
    {
        SystemUsers UsersCollection = null;
        protected readonly int LineLength = 3;

        public ProfitCenterFactory(Stream fileStream, string DBConnectionString, TypeOfDatabase DatabaseType)
            : base(fileStream, DBConnectionString, DatabaseType)
        {
            base.FileStreamObj = fileStream;
            base.DBConnectionString = DBConnectionString;
            base.DatabaseType = DatabaseType;
        }

        public override bool ReadFile()
        {
            bool smooth = true;

            if ((smooth = base.ReadFile()))
            {
                if (this.FileLines != null)
                {
                    this.UsersCollection = new SystemUsers();

                    using (RawProfitCenterManager CurDataManager = new RawProfitCenterManager(this.CurrentSystemSession))
                    {
                        // execute pre-action before inject data
                        CurDataManager.ExecutePreInjectAction();
                        // start to analyze
                        for (int x = 1; x < this.FileLines.Count; x++) // x = 0 these is header file
                        {
                            try
                            {
                                string[] CurrentLine = this.FileLines[x].ToString().Split(',');
                                // break by offset
                                if (CurrentLine.Length >= this.LineLength)  // 3 is the only items
                                {
                                    string EntityID = CurrentLine[0];    // size: 2
                                    string CCCode = CurrentLine[1];    // size: 50
                                    string Description = (CurrentLine[2].Length > 100) ? CurrentLine[2].Substring(0, 100).Trim() : CurrentLine[2];    // size: 100
                                    CurDataManager.AddData(EntityID, CCCode, Description);
                                }
                            }
                            catch (Exception ex)
                            {
                                smooth = false;
                                this.ErrMsg = "[ProfitCenterFactory.ReadFile] : Exception : " + ex.Message;
                            }
                        }   // for

                        // inject now
                        if (CurDataManager.TotalInTheList > 0)
                        {
                            CurDataManager.ExecuteFinalAction();

                            if (!CurDataManager.ExecuteDataInject())
                            {
                                smooth = false;
                                this.ErrMsg = "[ProfitCenterFactory.ReadFile] : CurDataManager.ExecuteDataInject() : " + CurDataManager.ErrorMessage;
                            }
                        }
                    }
                }
            }

            return smooth;
        }
    }
    #endregion

}
